﻿//Ana Isabel Ibáñez García
//version 1:create the window with the controls
//version 7: added: second constructor to modifyMode, btnAddParameter_Click, AddParameter, ModifyParameter
//btnModifyParam_Click, btnAccept_Click, btnCancel_Click, btnDeleteParam_Click

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UMLDesigner
{
    public partial class MethodsWindow : Form
    {
        List<ParameterData> parameters;
        Boolean modifyMode = false;
        public MethodsWindow()
        {
            InitializeComponent();
            listBoxReturnType.DataSource = DataType.GetTypes();
            listBoxVisibility.DataSource = VisibilityType.GetTypes();
            parameters = new List<ParameterData>();
        }

        public MethodsWindow(MethodData met)
            : this()
        {
            modifyMode = true;
            textBoxName.Text = met.GetName();
            for (int i = 0; i < met.GetParameters().Count; i++)
            {
                parameters.Add(met.GetParameters()[i]);
                listBoxParameters.Items.Add(met.GetParameters()[i].GetUMLRepresentation());
            }
            listBoxReturnType.Text = met.GetReturnType();
            listBoxVisibility.Text = met.GetVisibility();
        }

        private void btnAddParameter_Click(object sender, EventArgs e)
        {
            ParametersWindow anyadirParametro = new ParametersWindow();
            anyadirParametro.Owner = this;
            anyadirParametro.Show(this);
        }

       

        public void AddParameter(string nombre, string tipo)
        {
            ParameterData parametro = new ParameterData(nombre, tipo);
            parameters.Add(parametro);
            listBoxParameters.Items.Add(parametro.GetUMLRepresentation());
        }

        public void ModifyParameter(string nombre, string tipo)
        {
            int indice = listBoxParameters.SelectedIndex;
            parameters[indice].SetName(nombre);
            parameters[indice].SetParamType(tipo);
            listBoxParameters.Items[indice] = parameters[indice].GetUMLRepresentation();
        }

      

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (!modifyMode)
            {
                WindowsManager.AddMethodToForm(listBoxVisibility.Text, textBoxName.Text, parameters, listBoxReturnType.Text);
            }
            else
            {
                WindowsManager.ModifyMethod(listBoxVisibility.Text, textBoxName.Text, parameters, listBoxReturnType.Text);
            }

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }


        private void btnModifyParam_Click(object sender, EventArgs e)
        {
            if (listBoxParameters.SelectedItem != null)
            {

                int index = listBoxParameters.SelectedIndex;
                ParametersWindow modifyParameter = new ParametersWindow(parameters[index]);
                modifyParameter.Owner = this;
                modifyParameter.Show(this);
            }
        }

        private void btnDeleteParam_Click(object sender, EventArgs e)
        {
            if (listBoxParameters.SelectedItem != null)
            {
                for (int i = 0; i < parameters.Count; i++)
                {

                    if (parameters[i].GetUMLRepresentation() == listBoxParameters.Text)
                    {
                        parameters.Remove(parameters[i]);
                    }
                }

                listBoxParameters.Items.Remove(listBoxParameters.SelectedItem);

            }
        }
    }
}
